package com.example.questionnaire.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.dao.AppidAndSecretMapper;
import com.example.questionnaire.dao.QuestionnaireMapper;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.pojo.DAO.ImageLinkDAO;
import com.example.questionnaire.pojo.DO.AppidAndSecret;
import com.example.questionnaire.pojo.DO.Questionnaire;
import com.example.questionnaire.service.AppidAndSecretService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import static com.example.questionnaire.util.CommonUtil.*;

@RestController
@RequestMapping("/PictureController")
@Api(tags = "自定义图文")
public class PictureController {

    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Autowired
    private  AppidAndSecretMapper appidAndSecretMapper;
    @Autowired
    AppidAndSecretService appidAndSecretService;

    /**
     * @param multipartFile（上传的图片）
     * @param formid（问卷id）
     * @param PhotoType（上传图片的类型id）
     *

     */

    @PostMapping("/fileUpload")
    @ApiOperation(value = "自定义图文上传", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse fileUpload(@RequestParam("file") MultipartFile multipartFile, @RequestParam String formid, @RequestParam int PhotoType) throws ParseException {
        QueryWrapper<AppidAndSecret> wrapper1 = new QueryWrapper<>();
        wrapper1.select("domain_name","image_stored");
        AppidAndSecret appidAndSecret = appidAndSecretMapper.selectOne(wrapper1);

        Map<Integer, String> maps = new HashMap<>();
        maps.put(1,"CustomizeTheGraphicPage/");
        maps.put(2,"img/");
        maps.put(3,"bgImg/");
        maps.put(4,"WeChatSharingPicture/");

        String ImageName = getRandomPassword();
        //存放的路径
        String ImageStored = appidAndSecret.getImageStored();
        //域名
        String domainName = appidAndSecret.getDomainName();

        ImageUpload(multipartFile, formid, ImageStored, maps.get(PhotoType), ImageName);


        String name = domainName + maps.get(PhotoType) + formid + "/" + ImageName +".jpg";

        ImageLinkDAO imageLinkDAO = new ImageLinkDAO();
        imageLinkDAO.setImageLink(name);

        if(PhotoType == 4){
            UpdateWrapper<Questionnaire> wrapper = new UpdateWrapper<>();
            wrapper.eq("formid", formid)
                    .set("img_url",name);
            if(questionnaireMapper.update(null, wrapper) < 1){
                throw new RuntimeException("更新失败");
            }
        }

        return ServerResponse.createBySuccess(imageLinkDAO);
    }
}
